// pages/edu/exam/photo/index.js
import {Photo} from '../../model/photo.js'
import WxValidate from '../../../../utils/WxValidate.js'
import { ExamCategory } from '../../model/exam_category.js'

var Validate = ""
const PhotoModel=new Photo()
const ExamCategoryModel=new ExamCategory()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['江苏省', '苏州市', '相城区'],
    customItem: '全部',
    formData:{},
    end_images:[],
    detail_images:[],
    is_auth:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '工程实例'
    })
    this._checkCategroy()

    this.initValidate()
  },

  /**
   * 验证是否有权限
   */
  _checkCategroy(){
    ExamCategoryModel.getChildList(5,(res)=>{
    },(res)=>{
      if(res.msg_code==80004){
        this.setData({is_auth:false,'describe':res.msg})
      }
    })
  },
  
  //----------------------------

  //验证函数
  initValidate: function () {
    const rules = {
      name: {
        required: true,
      },
      end_time: {
        required: true,
      }
    }
    const messages = {
      name: {
        required: '工程名称必填',
      },
      end_time: {
        required: '竣工日期必选',
      }
    }
    // 创建实例对象
    Validate = new WxValidate(rules,messages)
  },

  /**
   * 地区选择
   * @param {*} e 
   */
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },

  /**
   * 日期选择
   * @param {*} e 
   */
  bindDateChange:function(e){
    this.setData({
      date: e.detail.value
    })
  },

  /**
   * 工程名
   * @param {*} e 
   */
  bindName(e){
    let value=e.detail.detail.value
    this._setFormData('name',value);
  },

  /**
   * 备注
   * @param {*} e 
   */
  bindRemark(e){
    let value=e.detail.detail.value
    this._setFormData('remark',value);
  },

  /**
   * 表单提交
   */
  bindSubmit(){
    this._setFormData('province',this.data.region[0])
    this._setFormData('city',this.data.region[1])
    this._setFormData('area',this.data.region[2])
    this._setFormData('end_time',this.data.date)
    let formData=this.data.formData
    let end_images=this.data.end_images
    let detail_images=this.data.detail_images

     //校验表单
     if (!Validate.checkForm(formData)) {
      var error = Validate.errorList[0]
      wx.lin.showToast({
        title: error.msg,
        icon:'error'
      })
      return false
    }
    
    if(end_images.length==0){
      wx.lin.showToast({
        title: "请上传竣工效果图片",
        icon:'error'
      })
      return ;
    }

    if(detail_images.length==0){
      wx.lin.showToast({
        title: "请上传细节效果图片",
        icon:'error'
      })
      return ;
    }

    PhotoModel.setFormData(formData,(res)=>{

      let id=res.data.id
      let upUrl=PhotoModel.baseRestUrl+'/set_exam_photo_img/'+id
      let fieldData={field:'end_images'};
      PhotoModel.uploadDIY(upUrl,end_images,0,end_images.length,fieldData,()=>{
        fieldData={field:"detail_images"}
        PhotoModel.uploadDIY(upUrl,detail_images,0,detail_images.length,fieldData,()=>{
          wx.lin.showToast({
            title: "提交成功",
            icon:'success'
          })
          setTimeout(()=>{
            wx.navigateTo({
              url: '../index/index'
            });
          },1500)
        })
      });
    })
  },



  /**
   * 竣工图片
   * @param {*} e 
   */
  bindEndImages(e){
    let value=e.detail.all
    this.data.end_images=value
  },

  /**
   * 细节图片
   * @param {*} e 
   */
  bindDetailImages(e){
    let value=e.detail.all
    this.data.detail_images=value
  },

  /**
   * 移除图片
   * @param {*} e 
   */
  bindRemoveImages(e){
    let value=e.detail.all
    let field=e.currentTarget.dataset.field
    this.data[field]=value
  },



  /**
   * 设置表单数据
   * @param {*} key 
   * @param {*} data 
   */
  _setFormData(key,data){
    let formData=this.data.formData;
    formData[key]=data
    this.data.formData=formData
  }
  


})